﻿@{
    ViewBag.Title = "选择地址";
    Layout = "~/Views/Shared/_layoutPersonal.cshtml";
}
@Styles.Render("~/Content/css/location")
@Scripts.Render("~/bundles/selectCityHelper")
<article class="main">
    <nav class="mod_nav">
        <div class="lay_left">
            <a class="nav_back" onclick="turnBack()"><img src="/images/return_1.png" ontouchstart="replaceBackImage(this, 1)" ontouchend="replaceBackImage(this, 2)"></a>
            <span>选择地址</span>
        </div>
    </nav>

    <section class="mod_select">
        <a id="mySelect" class="mySelect">
            <span id="city"></span>
        </a>
        <img src="/images/item_collasped.png" />
        <input id="inputAddress" class="input_address" type="text" placeholder="请输入详细地址">
    </section>

    <section class="mod_address_location">
        <a class="btn_vending" onclick="trunBack()">
            <img src="/images/poi_icon.png" />
            <span class=" address">定位到当前位置</span>
        </a>
    </section>

    <section class="mod_address poiPoint addressListCon">
        <div class="goods_window">
            <div class="goods_doc">
                <ul id="addressListInfo"></ul>
            </div>
        </div>
    </section>

    <section class="mod_title">
        <span>历史位置</span>
    </section>

    <section class="mod_address historyPoint"></section>

    <section class="CitySelectBackgroud" onclick="cancelSelectCity()"></section>

    <section class="CitySelectLayout">
        <div id="CitySelectBtn" class="CitySelectBtn">
            <a id="btnCancelSelectCity">取消</a>
            <a id="btnSelectCity">确定</a>
        </div>
        <div id="CitySelectUnit" class="CitySelectList">
            <div class="CitySelectListSpaceLine"></div>
            <ul class="CityListItem"></ul>
        </div>
    </section>
</article>

<script>
    function trunBack() {
        window.location.href = '/Home/Index';
    }

    var scrollBegin = 0;
    var isPoiAll = false;
    var isPoiQuery = false;

    //滚动处理
    function iscroll(scrollWindow, scrollDoc, scrollCallBack) {
        $(scrollWindow).scroll(function () {
            var scrollTop = $(this).scrollTop();
            var windowHeight = $(this).height();
            var docHeight = $(scrollDoc).height();
            if (scrollTop >= (docHeight - windowHeight)) {
                if (scrollCallBack) {
                    scrollCallBack();
                };
            }
        });
    }
    window.onload = function () {
        UserInfoMgr.getHistoryPoiListData(showHistoryPoint, hideHistoryPoint);
        AddressMgr.getCityListData(setCityListLayout, showCurCity);
    }

    function clickLocation(url) {
        AddressMgr.useWXAddr();
        window.location.href = url;
    }

    function showHistoryPoint(historyPointList) {
        $(".mod_title").show();
        $(".historyPoint").show();
        $("ul.addressListInfo li").each(function () {
            $(this).remove();
        });
        if (historyPointList != null && typeof (historyPointList) != "undefined") {
            for (var i = 0; i < historyPointList.length; i++) {
                var $li = $('<li><a class=\"btn_vending\" onclick=\"choosePointAddress(\'' + historyPointList[i].name + '\',\'' + historyPointList[i].address + '\',' + historyPointList[i].gps.latitude + ',' + historyPointList[i].gps.longitude + ')\"><span>' + historyPointList[i].address + '</span></a></li>');
                $(".historyPoint").append($li);
            }
        } else {
            $(".mod_title").hide();
            $(".historyPoint").hide();
        }
    }

    function hideHistoryPoint() {
        $(".mod_title").hide();
    }

    function poiKeyChange() {
        //当前浏览器窗口高度 考虑到header、地址搜索栏和定位的高度
        var currentWindowHeight = $(window).height() - 170; //由于 .vender 的高度变化了
        //当前容器确定高度
        $(".addressListCon").css("height", currentWindowHeight + "px");
        var goodsWindow = $(".goods_window");
        var goodsDoc = $(".goods_doc");
        iscroll(goodsWindow, goodsDoc, function () {
            if (isPoiAll == true) {
                return;
            } else {
                if (isPoiQuery == false) {
                    scrollBegin++;
                    //此处处理POI滚动加载
                    var keyword = $("#inputAddress").val();
                    var city = $("#city").html();
                    searchPoiByKeyword(keyword, city, scrollBegin, false);
                }
            }
        });

        var keyword = $("#inputAddress").val();
        if (keyword == "") {
            $(".location").show();
            $(".addressListCon").hide();
            clearPOIList();
            UserInfoMgr.getHistoryPoiListData(showHistoryPoint, hideHistoryPoint);
        } else {
            $(".addressListCon").show();
            $(".location").hide();
            $(".mod_title").hide();
            $(".historyPoint").hide();
            var city = $("#city").html();
            searchPoiByKeyword(keyword, city, 0, true);
        }
    }

    $(document).ready(function () {
        $("#inputAddress").bind('input propertychange', poiKeyChange);
    });

    function clearPOIList() {
        scrollBegin = 0;
        isPoiAll = false;
        isPoiQuery = false;
        $("ul#addressListInfo li").each(function () {
            $(this).remove();
        });
    }

    function searchPoiByKeyword(keyword, city, pageNum, isClear) {
        isPoiQuery = true;
        url = 'http://api.map.baidu.com/place/v2/search?query=' + encodeURIComponent(keyword) + '&scope=2&region=' + encodeURIComponent(city) + '&page_size=10&page_num=' + pageNum + '&output=json&ak=UC03PvVN1AEqsb6csSzY6vIF&callback=?';
        $.getJSON(url, function (msg) {
            if (msg.message == "ok") {
                if (isClear) {
                    clearPOIList();
                }
                var myObj = eval(msg.results);
                for (var i = 0; i < myObj.length; i++) {
                    if (myObj[i].location != null && typeof (myObj[i].location) != "undefined" && typeof (myObj[i].name) != "undefined" && typeof (myObj[i].address) != "undefined") {
                        var $li = $('<li><a class=\"btn_vending_poi\" onclick=\"choosePointAddress(\'' + myObj[i].name + '\',\'' + myObj[i].address + '\',' + myObj[i].location.lat + ',' + myObj[i].location.lng + ')\"><span class=\"address1\">' + myObj[i].name + '</span><span class=\"address2\">' + myObj[i].address + '</span></a></li>');
                        $("#addressListInfo").append($li);
                    }
                }
                if (myObj.length < 10) {
                    isPoiAll = true;
                }
            } else {
                $("#addressListInfo").append($li);
            }
            isPoiQuery = false;
        });
    }

    function choosePointAddress(name, addr, lat, log) {
        AddressMgr.setCurAddr(name, addr, Number(lat), Number(log));
        window.location.href = '/Home/Index';
    }

    function setCityListLayout(cityList) {
        $("ul.CityListItem li").each(function () {
            $(this).remove();
        });
        var $li = $('<li>&nbsp;</li>');
        $(".CityListItem").append($li);
        for (var i = 0; i < cityList.length; i++) {
            if (i == 0) {
                var $li = $('<li class=\"CitySelectListFocus\">' + cityList[i] + '</li>');
                $(".CityListItem").append($li);
            } else {
                var $li = $('<li >' + cityList[i] + '</li>');
                $(".CityListItem").append($li);
            }
        }
    }

    function showCitySelection() {
        $('.CitySelectBackgroud').show();
        $('.CitySelectLayout').show();
        $('body').css('overflow', 'hidden');
    }

    function showCurCity(curCity) {
        $('#city').text(curCity);
    }

    $("#CitySelectBtn").on("touchend", function (e) {
        return false;
    });

    $("#btnSelectCity").on("touchend", function (e) {
        setSelectCity();
        return false;
    });
    $("#btnCancelSelectCity").on("touchend", function (e) {
        cancelSelectCity();
        return false;
    });
    $("#mySelect").on("touchend", function (e) {
        showCitySelection();
        return false;
    });

    function setSelectCity() {
        $('.CitySelectBackgroud').hide();
        $('.CitySelectLayout').hide();
        $('body').css('overflow', 'scroll');
        var curCity = $('.CityListItem').children('.CitySelectListFocus').text();
        AddressMgr.setCurCity(curCity);
        showCurCity(curCity);
    }

    function cancelSelectCity() {
        $('.CitySelectBackgroud').hide();
        $('.CitySelectLayout').hide();
        $('body').css('overflow', 'scroll');
    }

    function turnBack() {
        window.history.back();
    }
</script>
